//
//  HQHFreeWriteWebViewController.m
//  LastOne
//
//  Created by 小柠檬 on 15/11/6.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "HQHFreeWriteWebViewController.h"
#import "HTTPMethod.h"
#import "ChangeString.h"
#import "FitHeader.h"
#import "GiFHUD.h"
@interface HQHFreeWriteWebViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)UIWebView *webView;


@end

@implementation HQHFreeWriteWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getData];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    _webView = [[UIWebView alloc]init];
    _webView.frame = CGRectMake(0, 0, self.view.frame.size.width, (554 + 49) * FitHeight);
    _webView.delegate = self;
    
    [self.view addSubview:_webView];
    
    NSLog(@"***%@", _ID);
    
    [GiFHUD setGifWithImageName:@"loading.gif"];
}

-(void)getData
{
    [GiFHUD show];
    [HTTPMethod getDataByString:@"http://api2.pianke.me/article/info" BodyDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=137794B8-2578-4119-B189-3E532E7C0F5B&version=3.0.6", _ID]] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        [_webView loadHTMLString:[dataDic objectForKey:@"html"] baseURL:nil];
        NSDictionary *shareinfoDic = [dataDic objectForKey:@"shareinfo"];
        self.title = [shareinfoDic objectForKey:@"title"];
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [GiFHUD dismiss];
    NSString *script = [NSString stringWithFormat:@"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var myimg;"
                        "var maxwidth = %f;"//屏幕宽度
                        "for (i = 0; i < document.images.length; i++) {"
                        "myimg = document.images[i];"
                        "myimg.height = maxwidth / (myimg.width / myimg.height);"
                        "myimg.width = maxwidth;"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('p')[0].appendChild(script);", [UIScreen mainScreen].bounds.size.width - 15 * FitWidth];
    //添加JS
    [webView stringByEvaluatingJavaScriptFromString:script];
    //添加调用JS执行的语句
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        }
        else{
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
            [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        }
    
    
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
